package com.chhuang.ewcrm.controller;

import com.chhuang.ewcrm.bean.QueryBean;
import com.chhuang.ewcrm.entity.ESellGoods;
import com.chhuang.ewcrm.entity.ESellGoods;
import com.chhuang.ewcrm.service.ESellGoodsService;
import com.chhuang.ewcrm.vo.ResultBean;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * (ESellGoods)表控制层
 *
 * @author chhuang
 * @since 2020-11-14 13:21:48
 */
@RestController
@RequestMapping("/service/eSellGoods")
public class ESellGoodsController {
    Logger logger = LoggerFactory.getLogger(ESellGoodsController.class);
    /**
     * 服务对象
     */
    @Resource
    private ESellGoodsService eSellGoodsService;

    /**
     * 获取所有产品
     *
     * @return 单条数据
     */
    @GetMapping("/getAll")
    public ResultBean<List<ESellGoods>> getAll() {
        ResultBean<List<ESellGoods>> resultBean = new ResultBean<>();
        resultBean.setSuccess(true);
        resultBean.setData(this.eSellGoodsService.query(null));
        return resultBean;
    }

    /**
     * 删除产品
     * @param goodsId
     * @return
     */
    @DeleteMapping("/removeOne/{goodsId}")
    public ResultBean<?> removeOne(@PathVariable("goodsId") String goodsId){
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.setSuccess(true);
        if(StringUtils.isNotEmpty(goodsId)){
            //删除产品
            resultBean.setSuccess(this.eSellGoodsService.deleteById(goodsId));
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 根据id列表批量删除
     * @param goodsIds
     * @return
     */
    @PutMapping("/removeList")
    public ResultBean<?> removeList(@RequestBody List<String> goodsIds){
        ResultBean<?> resultBean = new ResultBean<>();
        if(goodsIds!=null && !goodsIds.isEmpty()){
            resultBean.setSuccess(this.eSellGoodsService.deleteByIds(goodsIds)>0);
        }else{
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 修改产品信息
     * @param eSellGoods
     * @return
     * @throws IOException
     */
    @PutMapping("/editOne")
    public ResultBean<ESellGoods> editOne(@RequestBody ESellGoods eSellGoods){
        ResultBean<ESellGoods> resultBean = new ResultBean<ESellGoods>();
        resultBean.setSuccess(false);
        if(eSellGoods!=null && StringUtils.isNotEmpty(eSellGoods.getGoodsName())) {
            if (StringUtils.isEmpty(eSellGoods.getGoodsId())) { //添加
                Date now = new Date();
                eSellGoods.setCreateTime(now);
                eSellGoods.setModifyTime(now);
                eSellGoods.setSellTotalWeight(0d);
                eSellGoods.setSellTotalAmount(0d);
                eSellGoods.setSellAveragePrice(0d);
                eSellGoods = this.eSellGoodsService.insert(eSellGoods);
                resultBean.setData(eSellGoods);
                resultBean.setSuccess(resultBean.getData()!=null);
            } else{ //修改
                ESellGoods sellGoods = eSellGoodsService.queryById(eSellGoods.getGoodsId());
                if (sellGoods != null) {
                    //修改产品信息
                    sellGoods.setGoodsName(eSellGoods.getGoodsName());
                    sellGoods.setWarehouseId(eSellGoods.getWarehouseId());
                    sellGoods.setWarehouseName(eSellGoods.getWarehouseName());
                    sellGoods.setModifyTime(new Date());
                    eSellGoods = this.eSellGoodsService.update(sellGoods);
                    resultBean.setData(eSellGoods);
                    resultBean.setSuccess(resultBean.getData()!=null);
                } else
                    resultBean.setMessage("没有找到产品");
            }
        } else
            resultBean.setMessage("参数不能为空");
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param goodsId 主键
     * @return 单条数据
     */
    @GetMapping("/getOne")
    public ResultBean<ESellGoods> getOne(String goodsId) {
        ResultBean<ESellGoods> resultBean = new ResultBean<>();
        resultBean.setSuccess(false);
        if(StringUtils.isNotEmpty(goodsId)){
            resultBean.setSuccess(true);
            resultBean.setData(this.eSellGoodsService.queryById(goodsId));
        }else{
            resultBean.setMessage("参数不为空");
        }
        logger.debug(resultBean.toString());
        return resultBean;
    }

    /**
     * 表格查询
     * @param queryBean
     * @return
     */
    @PostMapping("/queryPage")
    public ResultBean<PageInfo<ESellGoods>> query(@RequestBody QueryBean<ESellGoods> queryBean){
        logger.debug(queryBean.toString());
        ResultBean<PageInfo<ESellGoods>> resultBean = new ResultBean<>();

        PageInfo<ESellGoods> pageInfo = null;
        List<ESellGoods> sellGoodss = null;
        if(queryBean!=null) {
            if (StringUtils.isNotEmpty(queryBean.getOrderBy()))
                pageInfo = eSellGoodsService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize(), queryBean.getOrderBy());
            else
                pageInfo = eSellGoodsService.search(queryBean.getSearch(), queryBean.getPageNum(), queryBean.getPageSize());
            resultBean.setData(pageInfo);
            resultBean.setSuccess(true);
            resultBean.setCode(queryBean.getDraw());
        }else {
            resultBean.setSuccess(false);
            resultBean.setMessage("参数不为空");
        }

        logger.debug(resultBean.toString());
        return resultBean;
    }
}